<?php
class ProductController extends Controller{
    public function Upload($file){
        $fileupload = $file['file_upload'];
        $tmp=$fileupload['tmp_name'];
        $filename=time().$fileupload['name'];
        $destinal='uploads/product/'.$filename;
        move_uploaded_file($tmp,$destinal);
        return $destinal;
    }
    public function MultipleUpload($slide,$id_product){
        foreach($slide['name'] as $key =>$value){
            $fileupload = $slide;
            $tmp=$fileupload['tmp_name'][$key];
            $filename=time().$fileupload['name'][$key];
            $destinal='uploads/slide/'.$filename;
            move_uploaded_file($tmp,$destinal);
            $modelimages = new Images();
            $modelimages ->destination = $destinal;
            $modelimages ->product_id = $id_product;
            $modelimages->save();
        }
    }
    public $layout='//layouts/admin';
    //public $layout='//layouts/home';

    public function Listcategory(){
        $model = new Category();
        $listmodel = $model->findAll();
        return $listmodel;
    }

    public function actionIndex(){
        $this->render('index');
    }

    public function actionPreInsertProduct(){
        session_start();
        if(isset($_SESSION['admin'])){
            $modelType = new Type();
            $listtype = $modelType->findAll(array('order'=>'id asc'));
            $listcategory = $this->Listcategory();
            $model = new Product();
            $this->render('insertproduct',array('listcategory'=>$listcategory,'model' => $model,'listtype'=>$listtype));
        }else{
            $this->redirect('index.php?r=admin/admin/prelogin');
        }
    }

    public function actionInsertProduct(){
        session_start();
//        Echo '<pre>';
//        var_dump($_POST);
//        die;

        if(isset($_SESSION['admin'])){
            $images = $this->Upload($_FILES);
            $tockenkey = substr(time(),4,7).rand(0,99999);


            $model = New Product();
            $model->name = $_POST['name'];
            $model->info = $_POST['Product']['info'];
            $model->cost = $_POST['cost'];
            $model->images = $images;
            $model->category_id = $_POST['category_id'];
            $model->type_id =$_POST['type_id'];
            $model->admin_id=$_SESSION['admin']['id'];
            $model->tockenkey = $tockenkey;
            $check = $model->save();
            $product = $model->findByAttributes(array('tockenkey'=>$tockenkey));
            $id_product = $product->id;
            $slide = $_FILES['file_upload_slide'];
            $this->MultipleUpload($slide,$id_product);//upload hang loat
            if($check==true){
                Yii::app()->user->setFlash('contact','Thêm sản phẩm thành công');
                $this->redirect('index.php?r=admin/product/index');
            }else{
                Yii::app()->user->setFlash('contact','Thêm sản phẩm thất bại');
                $this->redirect('index.php?r=admin/product/index');
            }
        }

    }
    public function actionListProduct(){
        $model = new Product();
        $listproduct = $model->findAll();

        $sumproduct = count($listproduct);
        $countproduct = 9;
        $sumpage = ceil($sumproduct/$countproduct);


        isset($_GET['pageid'])? $pageid = $_GET['pageid'] : $pageid = 0;
        $offset = $pageid*$countproduct;
        $listproduct = $model->findAll(array('limit' => "$countproduct",'offset' => "$offset"));
        $this->render('listproduct', array('listproduct'=>$listproduct,'sumpage'=>$sumpage));

    }
    public function actionDeleteProduct(){
        $id = $_GET['id'];
        session_start();
        $model = Product::model()->FindByPk($id);
        $model->admin_id;
        if($_SESSION['admin']['lever']==1 || $model->admin_id == $_SESSION['admin']['id']){
            $check = $model->delete();
            Yii::app()->user->setFlash('contact','xóa thành công');
            $this->redirect('index.php?r=admin/product/listproduct');
        }
        //    if($model->admin_id == $_SESSION['admin']['id']){
//            $check = $model->delete();
//            Yii::app()->user->setFlash('contact','xóa thành công');
//            $this->redirect('index.php?r=admin/product/listproduct');
//        }
        else{
            Yii::app()->user->setFlash('contact','xóa thất bại');
            $this->redirect('index.php?r=admin/product/listproduct');
        }
    }
    public function actionPreEditProduct(){
        session_start();
        is_numeric($_GET['id'])?$id = $_GET['id']: $id = null;
        $model = Product::model()->FindByPk($id);
        $model->admin_id;

        if($_SESSION['admin']['lever']==1 ||$model->admin_id == $_SESSION['admin']['id']){
            $model = New Product();
            $itemproduct = $model = Product::model()->FindByPk($id);
            echo $_SESSION['product']['id']= $id;
            $listcategory = $this->Listcategory();
            $modelimages = New Images();
            $images = $modelimages= Images::model()->findAll("product_id=$id");
            $modeltype = new Type();
            $listtype = $modeltype->findAll();
            $this->render('editproduct',array('itemproduct'=>$itemproduct,'model' => $model,'listcategory'=>$listcategory,'images'=>$images, 'listtype'=>$listtype));
        }else{
            Yii::app()->user->setFlash('contact','Không được sửa');
            $this->redirect('index.php?r=admin/product/listproduct');
        }
    }
    public function actionEditProduct(){
        session_start();
        $id =  $_SESSION['product']['id'];
        $slide = $_FILES['file_upload_slide'];

        if($_FILES['file_upload_slide']['name'][0]!=null){
            $model_images = new Images();
            $model_images = $this->MultipleUpload($slide,$id);
            $model_images->save;
        }
        if($_FILES['file_upload']['name']!=null){
            $images = $this->Upload($_FILES);
        }



        $model = Product::model()->FindByPk($id);
        $model->name = $_POST['name'];
        $model->info = $_POST['Product']['info'];
        $model->cost = $_POST['cost'];
        if($_FILES['file_upload']['name']!=null){
            $model->images = $images;
        }
        $model->category_id = $_POST['category_id'];
        $model->type_id =$_POST['type_id'];
        $model->admin_id=$_SESSION['admin']['id'];
        $model->update();
        Yii::app()->user->setFlash('contact','Cập nhật thành công');
        $this->redirect('index.php?r=admin/product/listproduct');

    }


    public function actionDeleteImageSlide(){
        session_start();
        echo $idproduct =  $_SESSION['product']['id'];
        echo $id = $_GET['id'];
        $model = Images::model()->FindByPk($id);
        $model->delete();
        $this->redirect("index.php?r=admin/product/preeditproduct&id=$idproduct");
    }

}